package com.atguigu.lease.app.controller;

import com.atguigu.lease.app.service.LoginService;
import com.atguigu.lease.common.consts.StgyAdminConstant;
import com.atguigu.lease.common.handler.StgyAdminAuthInterceptor;
import com.atguigu.lease.common.handler.StgyAppAuthInterceptor;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.common.util.JwtUtil;
import com.atguigu.lease.model.bean.UserInfo;
import io.jsonwebtoken.impl.DefaultClaims;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "登录模块")
@RestController
@RequestMapping("/app")
public class LoginController {
    @Autowired
    LoginService loginService;
    @Operation(summary = "3、获取用户信息")
    @GetMapping("/info")
    public Result info(@RequestHeader("ACCESS_TOKEN")String token){
        // 这个拦截器拦截的是/admin开始的路径！
//        StgyAdminAuthInterceptor.SYSTEM_USER_THREAD_LOCAL.get()
//        DefaultClaims claims = JwtUtil.parseToken(token);
//
//        String nickName = claims.get(StgyAdminConstant.JWT_TOKEN_USER_NAME, String.class);
//        String avatarUrl = claims.get(StgyAdminConstant.JWT_TOKEN_AVATAR_URL, String.class);

        UserInfo userInfo = StgyAppAuthInterceptor.APP_USER_THREAD_LOCAL.get();
        Map map = new HashMap();
        map.put("nickname", userInfo.getNickname());
        map.put("avatarUrl", userInfo.getAvatarUrl());

        return Result.ok(map);
    }
    @Operation(summary = "2、登录")
    @PostMapping("/login") // 请求体提交json
    public Result<String> login(@RequestBody Map<String,String> loginVo){
        String token = loginService.login(loginVo);
        return Result.ok(token);
    }


    @Operation(summary = "1、获取短信验证码")
    @GetMapping("/login/getCode")
    public Result getCode(@RequestParam String phone){
        loginService.getCode(phone);
        return Result.ok();
    }

}
